﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using LibNiconico.NiconicoApi.Internal;
using LibNiconico.NiconicoApi.Extentions;
using LibNiconico.NiconicoApi.Exceptions;


namespace LibNiconico.NiconicoApi.Videos.ThumbInfo
{
    /// <summary>
    /// ニコニコ動画情報
    /// </summary>
    public sealed class NicoVideoThumbInfo
    {

        /// <summary>
        /// 動画番号
        /// </summary>
        public string VideoId
        {
            get;
            private set;
        }

        /// <summary>
        /// タイトル
        /// </summary>
        public string Title
        {
            get;
            private set;
        }

        /// <summary>
        /// 詳細
        /// </summary>
        public string Description
        {
            get;
            private set;
        }

        /// <summary>
        /// サムネイルURL
        /// </summary>
        public Uri ThumbnailUrl
        {
            get;
            private set;
        }

        /// <summary>
        /// 不明
        /// </summary>
        public DateTime FirstRetrieve
        {
            get;
            private set;
        }

        /// <summary>
        /// 動画時間
        /// </summary>
        public TimeSpan Length
        {
            get;
            private set;
        }

        /// <summary>
        /// 動画タイプ
        /// </summary>
        public string MovieType
        {
            get;
            private set;
        }


        /// <summary>
        /// ？
        /// </summary>
        public int SizeHigh
        {
            get;
            private set;
        }

        /// <summary>
        /// ？
        /// </summary>
        public int SizeLow
        {
            get;
            private set;
        }

        /// <summary>
        /// 視聴者数
        /// </summary>
        public int ViewCounter
        {
            get;
            private set;
        }

        /// <summary>
        /// コメント数
        /// </summary>
        public int CommentNum
        {
            get;
            private set;
        }

        /// <summary>
        /// マイリスト登録数
        /// </summary>
        public int MyListCounter
        {
            get;
            private set;
        }

        /// <summary>
        /// 最後につけられたコメント
        /// </summary>
        public string LastResBody
        {
            get;
            private set;
        }

        /// <summary>
        /// 視聴ページURL
        /// </summary>
        public Uri WatchUrl
        {
            get;
            private set;
        }

        /// <summary>
        /// thumbType
        /// </summary>
        public string ThumbType
        {
            get;
            private set;
        }

        /// <summary>
        /// 埋め込み可能か判定
        /// </summary>
        public bool Embeddable
        {
            get;
            private set;
        }

        /// <summary>
        /// 生放送仕様不可か判定
        /// </summary>
        public bool NoLivePlay
        {
            get;
            private set;
        }


        /// <summary>
        /// 投稿者ユーザID
        /// </summary>
        public string UserId
        {
            get;
            private set;
        }

        /// <summary>
        /// タグ
        /// </summary>
        public IEnumerable<NicoVideoTags> Tags
        {
            get;
            private set;
        }




        private static TimeSpan ParseNicoVideoLngh(XElement element, string name)
        {
            try
            {
                return TimeSpanExtension.ParseFromNicoVideoLength(XElementExtention.GetElementAsString(element, name));
            }catch(InvalidCastException e){
                throw NicoApiException.CreateXmlParseException(name, e);
            }
        }

        internal static NicoVideoThumbInfo Parse(XDocument document)
        {
            return XElementExtention.Parse(document.Root, "thumb", item =>
            {
                return new NicoVideoThumbInfo
                {

                    VideoId = XElementExtention.GetElementAsString(item, "video_id"),
                    CommentNum = XElementExtention.GetElementAsInt(item, "comment_num"),
                    Description = XElementExtention.GetElementAsString(item, "description"),
                    Embeddable = XElementExtention.GetElementAsBool(item, "embeddable"),
                    FirstRetrieve = XElementExtention.GetElementAsDateTime(item, "first_retrieve"),
                    LastResBody = XElementExtention.GetElementAsString(item, "last_res_body"),
                    Length = ParseNicoVideoLngh(item, "length"),
                    MovieType = XElementExtention.GetElementAsString(item, "movie_type"),
                    MyListCounter = XElementExtention.GetElementAsInt(item, "mylist_counter"),
                    NoLivePlay = XElementExtention.GetElementAsBool(item, "no_live_play"),
                    SizeHigh = XElementExtention.GetElementAsInt(item, "size_high"),
                    SizeLow = XElementExtention.GetElementAsInt(item, "size_low"),
                    ThumbnailUrl = XElementExtention.GetElementAsUri(item, "thumbnail_url"),
                    ThumbType = XElementExtention.GetElementAsString(item, "thumb_type"),
                    Title = XElementExtention.GetElementAsString(item, "title"),
                    UserId = XElementExtention.GetElementAsString(item, "user_id"),
                    ViewCounter = XElementExtention.GetElementAsInt(item, "view_counter"),
                    WatchUrl = XElementExtention.GetElementAsUri(item, "watch_url"),
                    Tags = NicoVideoTags.Parse(item)


                };
            });
            
        }


        private NicoVideoThumbInfo()
        {

        }
    }
}
